
$(function () {
    getProvince();

    //注册时间
    $("#registrationTime").datepicker({
        language: "zh-CN",
        autoclose: true,
        clearBtn: true,
        todayHighlight: true,
        format: "yyyy-mm-dd"
    });
     //核准日期
        $("#approvedDate").datepicker({
            language: "zh-CN",
            autoclose: true,
            clearBtn: true,
            todayHighlight: true,
            format: "yyyy-mm-dd"
        });
        // 成立日期
        $('#establishmentDate').datepicker({
            language: "zh-CN",
            autoclose: true,
            clearBtn: true,
            todayHighlight: true,
            format: "yyyy-mm-dd"
        });
});
// 渲染城市列表
function getProvince() {
    let provinceNames = [];
    $.each(cityJson[0], function(key, value){
        provinceNames.push('<option value="' + key + '">' + value + '</option>')
    });
    $('#province').html(provinceNames.join(''));
    getCity('110000');
}


// 根据省获取市
function getCity(provinceId) {
    $('#city').html('');
    let key = '0,'+provinceId;
    let cityNames = [];
    $.each(cityJson[key], function(key, value){
        cityNames.push('<option value="' + key + '">' + value + '</option>')
    });
    $('#city').html(cityNames.join(''));
}
// '切换城市'
function changeCity() {
    getCity($('#province').val())
}
function addPublicClue(){
    var poolId = "";
    var poolCategory = $("#poolCategory").val();
    var poolState = $("#poolState").val();
    var defaultMark = $("#defaultMark").val();
    var allocation = $("#allocation").val();
    var companyId = $("#companyId").val();

    <!--是否显示权限与详情tab页-->
    var poolPermissionFlag = false;
    poolPermissionFlag = $("#poolPermissionFlag").val();

    //线索池分类
    var poolCategory = $("#poolCategory").val();

    if(poolCategory != '0'){
        //公共线索池id
        var cluePoolPublicId = $("#poolId").val();
        var clueType = "1";
        poolId = cluePoolPublicId;
    }else {
        //私有线索池id
        var cluePoolPrivateId = $("#poolId").val();
        var clueType = "2";
        poolId = cluePoolPrivateId;
    }


    //公司名称
    var companyName = $.trim($("#companyName").val());

    //联系人
    var contactPerson = $.trim($("#contactPerson").val());

    //联系电话
    var contactPersonPhone = $.trim($("#contactPersonPhone").val());

    //性别
    var sex = $("input[name='sex']:checked").val();

    //公司邮箱
    var companyEmail = $.trim($("#companyEmail").val());

    //职务
    var job = $.trim($("#job").val());

    //线索评级
    var rating = $('#rating').val();

    //线索来源
    var source = $.trim($("#source").val());

    //客户类别
    var clientCategories = $('#clientCategories').val();

    //客户级别
    var clientLevel = $('#clientLevel').val();

    //社会信用码
    var unifiedSocialCreditCode = $.trim($('#unifiedSocialCreditCode').val());

    //行业
    var industry = $.trim($('#industry').val());

    //公司规模
    var companySize = $('#companySize').val();

    //公司网址
    var companyWebsite = $.trim($('#companyWebsite').val());

    //注册号
    var registrationNumber = $.trim($('#registrationNumber').val());

    //组织机构代码
    var organizationCode = $.trim($('#organizationCode').val());

    //公司类型
    var companyType = $('#companyType').val();

    //法定代表人
    var legalRepresentative = $.trim($('#legalRepresentative').val());

    //对外手机
    var enterpriseExternalPhone = $.trim($('#enterpriseExternalPhone').val());

    //对外座机
    var enterpriseExternalLandline = $.trim($('#enterpriseExternalLandline').val());

    //注册资本
    var registeredCapital = $.trim($('#registeredCapital').val());

    //经营范围
    var businessScope = $.trim($('#businessScope').val());

    //注册时间
    var registrationTime = $.trim($('#registrationTime').val());

    //资本类型
    var capitalType = $.trim($('#capitalType').val());

    //企业状态
    var enterpriseState = $('#enterpriseState').val();

    //参保人数
    var insuredNumber =  $.trim($('#insuredNumber').val());

    //微信
    var wechat = $.trim($('#wechat').val());

    //备注
    var remark = $.trim($('#remark').val());

    //工商注册号
    var businessRegistrationNumber = $.trim($('#businessRegistrationNumber').val());

    //实缴资本
    var paidInCapital = $.trim($('#paidInCapital').val());

    //纳税人识别号
    var taxpayerIdentificationNumber = $.trim($('#taxpayerIdentificationNumber').val());

    //登记机关
    var registrationAuthority = $.trim($('#registrationAuthority').val());

    //核准日期
    var approvedDate = $('#approvedDate').val();

    //成立日期
    var establishmentDate = $('#establishmentDate').val();

    //进出口企业代码
    var importAndExportEnterpriseCode = $.trim($('#importAndExportEnterpriseCode').val());

    //企业地址
    var companyAddress = $.trim($('#companyAddress').val());

    //线索标签
    var tagStr = $('.fm-button').text().split(' ').join(',');
    var tagStrNew = tagStr.substring(0, tagStr.lastIndexOf(','))+ tagStr.substring(tagStr.lastIndexOf(',')+1);
    var tag = tagStrNew;

    //省份id
    var provinceId = $('#province').val();

    //省份名称
    var provinceName = $("#province").find("option:selected").text();

    //城市id
    var cityId = $('#city').val();

    //城市名称
    var cityName = $("#city").find("option:selected").text();

    //公司名称为空和长度验证
    if (!companyName) {
        promptInfo("公司名称不能为空!");
        return;
    } else {
        if (companyName.length > 20) {
            promptInfo("公司名称长度不能大于20个字！");
            return;
        }
    }

    //联系人
    if (!contactPerson) {
        promptInfo("联系人不能为空!");
        return;
    }

    //线索来源
    if (!source) {
        promptInfo("线索来源不能为空!");
        return;
    }



    //联系人电话
/*        if (contactPersonPhone.length > 64) {
            ok = false;
            promptInfo("联系电话长度不能大于64个字符！");
            return;
        } else {
            let reg = /^1[3|4|5|7|8][0-9]{9}$/;
            if (!reg.test(contactPersonPhone)) {
                ok = false;
                promptInfo("联系电话不存在！");
                return;
            }
        }*/

    // 公司邮箱
    if (companyEmail.length > 64) {
        ok = false;
        promptInfo("电子邮件长度不能大于64个字符！");
        return;
    } else if(64 > companyEmail.length > 1){
        let reg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
        if (!reg.test(companyEmail)) {
            ok = false;
            promptInfo("请输入正确的邮箱格式！");
            return;
        }
    }

    //社会信用码
    if (!unifiedSocialCreditCode) {
        promptInfo("社会信用码不能为空!");
        return;
    } else if (unifiedSocialCreditCode.length > 20||unifiedSocialCreditCode.length <15) {
        promptInfo("社会信用码长度在15-20位以内！");
        return;
    }

    var jsonData = {
        "cluePoolPublicId":cluePoolPublicId,
        "cluePoolPrivateId":cluePoolPrivateId,
        "companyName":companyName,
        "contactPerson":contactPerson,
        "contactPersonPhone":contactPersonPhone,
        "sex":sex,
        "companyEmail":companyEmail,
        "job":job,
        "rating":rating,
        "source":source,
        "clientCategories": clientCategories,
        "clientLevel":clientLevel,
        "unifiedSocialCreditCode":unifiedSocialCreditCode,
        "industry":industry,
        "companySize":companySize,
        "companyWebsite":companyWebsite,
        "registrationNumber":registrationNumber,
        "organizationCode":organizationCode,
        "companyType":companyType,
        "legalRepresentative":legalRepresentative,
        "enterpriseExternalPhone":enterpriseExternalPhone,
        "enterpriseExternalLandline":enterpriseExternalLandline,
        "registeredCapital":registeredCapital,
        "businessScope":businessScope,
        "registrationTime":registrationTime,
        "capitalType":capitalType,
        "enterpriseState":enterpriseState,
        "insuredNumber":insuredNumber,
        "wechat":wechat,
        "remark":remark,
        "businessRegistrationNumber":businessRegistrationNumber,
        "paidInCapital":paidInCapital,
        "taxpayerIdentificationNumber":taxpayerIdentificationNumber,
        "registrationAuthority":registrationAuthority,
        "approvedDate":approvedDate,
        "establishmentDate":establishmentDate,
        "importAndExportEnterpriseCode":importAndExportEnterpriseCode,
        "companyAddress":companyAddress,
        "tag":tag,
        "provinceId":provinceId,
        "provinceName":provinceName,
        "cityId":cityId,
        "cityName":cityName,
        "clueState":0,//在池
        "menuIdentify":2, //管理端
        "clueType":clueType //线索池类型：1：公有线索池；2：私有线索池

    };

    $.ajax({
        url: '/clue/addClue',
        type: 'POST',
        dataType: "json",
        contentType: "application/json;charset=utf-8",
        data: JSON.stringify(jsonData),
        success: (function (data) {
            if(data.code == '000000'){
                $.alert({
                    title: '提示',
                    content: '保存成功！',
                    buttons: {
                        confirm: {
                            text: '确认',
                            btnClass: 'btn-primary',
                            action: function () {
                                var $thisTabs = parent.$('.mt-nav-bar .nav-tabs').find('li.active');
                                var navTab = parent.$multitabs._getNavTab($thisTabs);
                                closeTab(navTab);
                                openTab("线索池详情", "/front/cluePoolManager/cluePoolDetail?poolId=" + poolId + "&poolCategory=" + poolCategory + "&poolState=" + poolState + "&defaultMark=" + defaultMark + "&allocation=" + allocation
                                    + "&companyId=" + companyId + "&poolPermissionFlag=" + poolPermissionFlag);
                                // parent.$(parent.document).data('multitabs').create({
                                //     iframe : true,
                                //     title : '线索池详情',
                                //     url : "/front/cluePoolManager/cluePoolDetail?poolId=" + poolId + "&poolCategory=" + poolCategory + "&poolState=" + poolState + "&defaultMark=" + defaultMark + "&allocation=" + allocation
                                //         + "&companyId=" + companyId + "&poolPermissionFlag=" + poolPermissionFlag
                                // }, true);
                                // 提交完表单，重新加载相关table列表页面
                                refreshActivePane("/front/cluePoolManager/cluePoolDetail?poolId=" + poolId + "&poolCategory=" + poolCategory + "&poolState=" + poolState + "&defaultMark=" + defaultMark + "&allocation=" + allocation
                                    + "&companyId=" + companyId + "&poolPermissionFlag=" + poolPermissionFlag);
                                delStorageAndRemove(navTab);
                            }
                        }
                    }
                });
            } else if(data.code == 'C00020'){
                //线索名称重复
                $.alert(data.message);
            } else if(data.code == 'C00011'){
                //超出线索池容量
                $.alert(data.message);
            } else {
                $.alert("保存失败");
            }
        }),
        error: (function (res) {
            $.alert("保存失败");
        })
    })
}

/**
 * 提示信息
 * @param content  提示内容
 */
function promptInfo(content) {
    $.alert({
        title: '温馨提示',
        content: content,
        buttons: {
            confirm: {
                text: '确认',
                btnClass: 'btn-primary'
            }
        }
    });
}